import api from "../api/api";
// import store from "../store";//这个加不加无所谓

// console.log(api);
export default {
    //首页需要的请求管理方法
    // loadHotData(store,arg) {
    loadHotData({commit},arg) {
        // console.log(arg);
        api.loadMutigetData({
            appPlat: "m",
            pids: "106930,51836,51833",
            callbackQuery: "callback"
        }).then((data) => {
            // store.state.homePage.goodTypes = data["106930"].list;
            // store.state.homePage.hotTypes = data["51836"].list;
            // store.state.homePage.shihuiTypes = data["51833"].list;
            commit("setHomeGoodTypes",data["106930"].list);
            commit("setHomeHotTypes",data["51836"].list);
            commit("setHomeShihuiTypes",data["51833"].list);
        }).catch(api.errorFun);
        // }).catch(this.$showErrorTip);
        // }).catch(showErrorTip);
    },
    loadLikeData({commit,dispatch}) {//commit、dispatch分别是从store仓库中结构出来的属性
        api.loadSearchData({
            pid: "9750",
            sort: "pop",
            cKey: 46
        }).then((result) => {
            // console.log(result);
            // store.state.homePage.likeTypes = result.wall.docs;
            commit("setHomeLikeTypes",result.wall.docs);
        }).catch(api.errorFun);
    },

    //分类页需要的请求管理方法
    loadTypeLists({commit,dispatch}){
        api.loadMutigetData({
            appPlat:"m",
            pids:"117330"
        }).then((data)=>{
            // console.log(data);
            commit("setClassTypeLists",data["117330"].list);
            if(data["117330"].list.length!=0){
                return { pid : data["117330"].list[0].maitKey ,fcid : data["117330"].list[0].miniWallkey };//返回的return值作为下一个then的参数
            }
        }).then(( { pid ,fcid} )=>{
            dispatch("loadTypesByMaitKey",pid);
            dispatch("loadGoodListByMiniWallKey",{fcid:fcid,sort:"pop"});
        })
        .catch(api.errorFun);
    },
    //右侧上方分类商品请求
    loadTypesByMaitKey({commit},arg1){
        api.loadTypeListsData({
            appPlat:"m",
            pid:arg1
        }).then((data)=>{
            // console.log(data);
            commit("setClassHotTypes",data.list);
        }).catch(api.errorFun);
    },
    //右侧产品列表及排序请求
    loadGoodListByMiniWallKey({commit},parmas){
        api.loadSearchData({
            ...parmas,
            page:1,
            cKey:"h5-cube"
        }).then((data)=>{
            // console.log(data);
            commit("setClassLikeTypes",data.wall.docs);
        }).catch(api.errorFun);
    },


    //首页和分类页面的hotBar点击进入searchGood页面的请求
    loadGoodsByTitle({commit},params){
        api.loadSearchData({
            ...params,
            cKey:16,
            page:1
        }).then((data)=>{
            // console.log(data);
            commit("setSearchGoodTypes",data.wall.docs);
        }).catch(api.errorFun);
    },

    //全网搜索以输入内容进行请求
    loadGoodsByWord({commit},params){
        api.loadSearchData({
            ...params,
            cKey:46,
            page:1,
        }).then((data)=>{
            // console.log(data);
            commit("setSearchGoodTypes",data.wall.docs);
        }).catch(api.errorFun);
    },

    //搜索关键字查询的请求
    loadQuerySearchKey({commit}){
        api.loadSearchKey({
            platform:"H5",
            code:"hotWord,sketch"
        }).then(({hotWord,sketch})=>{
            // console.log(data);
            commit("setSearchBarHotWord",hotWord.data);
            commit("setSearchBarSketch",sketch.data.query);
        }).catch(api.errorFun);
    },

    //查询物品评论请求
    loadGoodCommentsByiid({commit},arg){
        api.loadGoodComment({
            appPlat:"m",
            type:1,
            itemId:arg
        }).then((data)=>{
            // console.log(data);
            commit("setDetailPageGoodDetail",data||false);
        }).catch(api.errorFun);
    },

    //加入购物车方法
    addGoodToCart({commit,state},params){
        let iid=params.iid;
        let carts=state.cartPage.cartList;
        if(carts[iid]){
            carts[iid].count +=1;//判断carts对象中有无该iid属性，若有iid属性对象中加上count属性以存数量
        }else{
            params.count=1;
            carts[iid]=params;//给carts这个对象中添加iid属性，该属性值就是传入的对象
        };
        commit("setCartPageCartList",carts);
        sessionStorage.setItem("carts",JSON.stringify(carts));
    }
}
